package com.ck.controller.teacher;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.ck.domain.Chapter;
import com.ck.domain.Panduan;
import com.ck.service.QuestionService;

public class AddNewPanduanController extends SimpleFormController {
	
	private QuestionService questionService;

	public void setQuestionService(QuestionService questionService) {
		this.questionService = questionService;
	}

	//公共变量，用于表单回显
	private List<Chapter> chapterList = null;
	
	@Override
	protected Map referenceData(HttpServletRequest request, Object command,
			Errors errors) throws Exception {
		Map model = new HashMap();
		chapterList = questionService.gainChapterList();
		model.put("chapterList", chapterList);
		return model;
	}
	
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		
		Panduan panduan = (Panduan) command;
		Map model = errors.getModel();
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		
		//后台校验
		boolean flag = true;
		String description = panduan.getDescription().trim();
		Integer correct_answer = panduan.getCorrect_answer();
		Integer difficulty = panduan.getDifficulty();
		Integer score = panduan.getScore();
		Integer chapterID = panduan.getChapter();
		String remark = panduan.getRemark().trim();
		
		if(description == null || "".equals(description)) {
			model.put("error_description", "请输入题目描述");
			flag = false;
		}
		
		if(description.length() > 500) {
			model.put("error_description", "题目描述长度：1-500个字符");
			flag = false;
		}
		
		if(correct_answer == null || "".equals(correct_answer)) {
			model.put("error_correct_answer", "请选择正确答案");
			flag = false;
		}
		
		if( difficulty == 0){
			model.put("error_difficulty", "请选择难度");
			flag = false;
		}
		
		if(score == null){
			model.put("error_score", "请输入分值");
			flag = false;
		}else{
			if(score<1 || score>99){
				model.put("error_score", "分值须为1-99的整数");
				flag = false;
			}
		}
		
		if( chapterID == 0){
			model.put("error_chapterID", "请选择章节");
			flag = false;
		}
		
		if( remark == null || "".equals(remark) ){
			panduan.setRemark(null);
        }else{
        	if(remark.length() > 100) {
    			model.put("error_remark", "备注长度：0-100个字符");
    			flag = false;
    		}
        }
		
		//校验不通过
        if(!flag){
        	model.put("description", description);
        	model.put("correct_answer", correct_answer);
        	model.put("difficulty", difficulty);
        	model.put("score", score);
        	model.put("chapterID", chapterID);
        	model.put("remark", remark);
        	model.put("chapterList", chapterList);
			return new ModelAndView(getFormView(), model);
		}
		
		String pic = questionService.uploadImage(multipartRequest, model);
		panduan.setPic(pic);
		
		boolean addPanduanSuccess = questionService.addNewPanduan(panduan, model);
		
		if(!("error".equals(pic)) && addPanduanSuccess) {		
			return new ModelAndView(getSuccessView(),model);
		} else {
			model.put("description", description);
        	model.put("correct_answer", correct_answer);
        	model.put("difficulty", difficulty);
        	model.put("score", score);
        	model.put("chapterID", chapterID);
        	model.put("remark", remark);
			model.put("chapterList", chapterList);
			return new ModelAndView(getFormView(), model);
		}
	}
	
	

}
